import UserInfo from "@/components/UserInfo"
import {getDetailInfo, updateDetailInfo,} from "@/api/userInfo"
import {getMenu} from "@/api/studyspace"
export default {
  name: "index",
  components:{
    UserInfo
  },
  data(){
    return{
      menuList:[
        {
          name:"我的培训班",
          className:"my-trainclass",
          url:"my-trainclass",
          isPublish:"0",
        },
        {
          name:"我的课程",
          className:"my-required-course",
          url:"my-required-course",
          isPublish:"0",
        },
        {
          name:"历史课程",
          className:"my-history-course",
          url:"my-history-course",
          isPublish:"0",
        },
        {
          name:"在线考试",
          className:"my-exam",
          url:"my-exam",
          isPublish:"0",
        },
        {
          name:"我的消息",
          className:"my-notice",
          url:"my-notice",
          isPublish:"0",
        },
        {
          name:"订单管理",
          className:"my-order",
          url:"my-order",
          isPublish:"0",
        },
        {
          name:"购物车",
          className:"my-cart",
          url:"my-cart",
          isPublish:"0",
        },
        {
          name:"我的证书",
          className:"my-certificate",
          url:"my-certificate",
          isPublish:"0",
        },
        {
          name:"培训档案",
          className:"my-record",
          url:"my-record",
          isPublish:"0",
        },
      ],
      list:[],
      userInfo:{},
      dialogVisible:false,
    }
  },
  computed: {
    key() {
      return this.$route.name
    },
    imgUrl(){
      return function(className){
        if ( this.$store.state.app.activeMenu.indexOf(className) !== -1) {
          return require(`@/assets/images/${className}-logo-active.png`)
        } else {
          return require(`@/assets/images/${className}-logo.png`)
        }
      }
    },
    menuClass(){
      return (className)=>{
        if ( this.$store.state.app.activeMenu.indexOf(className) !== -1) {
          return `${className} active`;
        } else {
          return `${className}`;
        }
      }
    },

  },

  methods:{
    toUrl(e,path){
      /*if (path.indexOf('my-certificate') !== -1){
        if (this.$store.state.app.footer.certificateExternalLinks) {
          window.open(this.$store.state.app.footer.certificateExternalLinks, '_blank');
        }else {
          this.$router.replace(path)
        }
      } else {
        this.$router.replace(path)
      }*/
      if (path.indexOf('http') !== -1){
        window.open(path, '_blank');
      } else {
        this.$router.replace(path)
      }
    },
    getUserInfo(){
      getDetailInfo().then((response) =>{
        this.userInfo = response.data;
        var arr = this.userInfo.areaStr.split("_")
        this.userInfo.areaStr = arr[arr.length-1];
        this.dialogVisible = true;
      }).catch((err) =>{
        this.$message.error(err.message || 'error')
      });
    },
    handleUpdate(data){
      updateDetailInfo(data).then((response) =>{
        this.$message.success('修改成功')
        this.getUserInfo();
      }).catch((err) =>{
        this.$message.error(err.message || 'error')
      });
    },
    getMenuList(){
      var menuMap = new Map();
      var hrefArr = [];
      getMenu().then((response) =>{
        this.list = response.data;
        this.list && this.list.forEach((item,index) =>{
          menuMap.set(item.elementUrl, item);
          if (item.elementUrl.indexOf("http") !== -1 ){
            var hrefObj = {};
            hrefObj.isPublish = item.isPublish;
            hrefObj.url = item.elementUrl;
            hrefObj.name = item.elementName
            hrefObj.className = item.elementPosition
            hrefArr.push(hrefObj)
          }
        });
        this.menuList = this.menuList.concat(hrefArr);
        this.menuList.forEach((item,index) =>{
          var obj = menuMap.get(item.url);
          if (obj) {
            item.isPublish = obj.isPublish;
            item.name = obj.elementName;
          }
        })
        // console.log( this.menuList)
      }).catch((err) =>{
        this.$message.error(err.message)
      });
    }
  },
  mounted(){
    /*if (this.$store.state.app.footer.webTemplateType && this.$store.state.app.footer.webTemplateType.indexOf('V4') !== -1 && !this.$store.state.app.footer.certificateExternalLinks) {
      this.menuList.pop()
    }*/
    var type = this.$utils.getParms(this, 'type');
    if (type == 'userInfo') {
      this.getUserInfo();
    }
    this.getMenuList();


  }
}
